//
//  TSHomeViewController.m
//  chart
//
//  Created by xu on 15/8/3.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSHomeViewController.h"
#import "AppDelegate.h"
#import "TSUserInfoViewController.h"
#import "TSLoginViewController.h"
#import "YZSwipeBetweenViewController.h"
#import "TSPasswordEditViewController.h"

#define BUTTON_WIDTH 80.f
#define MARGIN_GAP_HOR 30.f
#define TITLE_HEIGHT 20.f

@interface TSHomeViewController ()

@end

@implementation TSHomeViewController
{
    NSArray *titleArray;
    NSArray *imageNameArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"主页菜单";
    
    WS(weakSelf);
    [self leftBarButtonWithTitle:nil barNorImage:[UIImage imageNamed:@"nav_back"] barHltImage:nil action:^{
        if (!weakSelf) {
            return;
        }
        
        [weakSelf onLeftBarButtonClick];
    }];
    
    [self rightBarButtonWithTitle:@"个人" barNorImage:nil barHltImage:nil action:^{
        if (!weakSelf) {
            return;
        }
        
        [weakSelf showUserInfoViewController];
    }];
    
    imageNameArray = @[@"icon_search_task", @"icon_submit_task", @"icon_statistics", @"icon_notification", @"icon_search", @"icon_other_function", @"icon_user_schedule", @"icon_project_track", @"icon_task_progress"];
    titleArray = @[@"任务查询", @"任务提交", @"统计分析", @"事件通知", @"智能查询", @"个人信息", @"个人日程", @"项目跟踪", @"任务进展"];
    
    [self addAllButtons:titleArray ImageNames:imageNameArray];
}

- (void)onLeftBarButtonClick
{
    if ([self isRootViewController]) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)addAllButtons:(NSArray *)titles ImageNames:(NSArray *)imageNames
{
    UIButton *btn;
    UILabel  *label;
    NSInteger gap = (currentDeviceWidth - 3*BUTTON_WIDTH)/4;
    for (int i=0; i<titles.count; i++) {
        
        btn = [self createHomeButton:CGRectMake(gap+(BUTTON_WIDTH+gap)*(i%3), MARGIN_TOP + (i/3) * (MARGIN_GAP_HOR + BUTTON_WIDTH), BUTTON_WIDTH, BUTTON_WIDTH) WithImageName:[imageNames objectAtIndex:i]];
        [btn setTag:i];
        [self.view addSubview:btn];
        
        label = [self createLabel:CGRectMake(CGRectGetMinX(btn.frame), CGRectGetMaxY(btn.frame), BUTTON_WIDTH, TITLE_HEIGHT) WithTitle:[titles objectAtIndex:i]];
        [label setTag:i];
        [self.view addSubview:label];
    }
}

- (UIButton *)createHomeButton:(CGRect)frame WithImageName:(NSString *)imageName
{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    return  btn;
}

- (void)onButtonClick:(UIButton *)sender
{
    /* 事件通知 */
    if (sender.tag == 0) {
        [self setTabBarSelectedItem:2];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if (sender.tag == 3) {
        [self setTabBarSelectedItem:1];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if (sender.tag == 6) {
        [self setTabBarSelectedItem:3];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if (sender.tag == 5) {
        [self showUserInfoViewController];
    }
    else{
        [TSTools showAlert:@"click"];
    }
}

- (void)showUserInfoViewController
{
//    TSUserInfoViewController *user_vc = [[TSUserInfoViewController alloc] init];
//    [self.navigationController pushViewController:user_vc animated:YES];
    
    
     YZSwipeBetweenViewController *scroll_vc =[YZSwipeBetweenViewController new];
     
     UIViewController *user_vc= [[TSUserInfoViewController alloc] init];
     UINavigationController *navCon1 =
     [[UINavigationController alloc]initWithRootViewController:user_vc];
     
     UIViewController *pwd_vc = [[TSPasswordEditViewController alloc] init];
     UINavigationController *navCon2 =
     [[UINavigationController alloc] initWithRootViewController:pwd_vc];
     scroll_vc.viewControllers = @[navCon1,navCon2];
     scroll_vc.initialViewControllerIndex=0;
     [self.navigationController pushViewController:scroll_vc animated:YES];
}

- (void)setTabBarSelectedItem:(NSInteger)index
{
    AppDelegate *app_delegate = [UIApplication sharedApplication].delegate;
    
    app_delegate.tabBarController.selectedIndex = index;
}

- (UILabel *)createLabel:(CGRect)frame WithTitle:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:title];
    [label setFont:FONT(12.f)];
    [label setTextAlignment:NSTextAlignmentCenter];
    
    return label;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
